var Weapon = require("Weapon");

var PlayerController = cc.Class({
    extends: cc.Component,

    properties: {
        playerNode: cc.Node,
        accel: 0,

        speed: 0,
        maxSpeed: 0,

        pickUpRange: 0,

        //activeWeapon: Weapon,

        assignedWeapons: [Weapon],
        unassignedWeapons: [Weapon],

        fullLevelWeapons: [Weapon],

        maxWeaponNumber: 3,
    },

    statics: {
        instance: null,
    },
    ctor () {
        PlayerController.instance = this;
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        cc.director.getPhysicsManager().enabled = true;

        cc.director.getCollisionManager().enabled = true;

        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        
        // 加速度方向开关
        this.accLeft = false;
        this.accRight = false;
        this.accUp = false;
        this.accDown = false;

        //移动方向开关
        this.moveUp = false;
        this.moveDown = false;
        this.moveLeft = false;
        this.moveRight = false;

        this.xSpeed = 0;
        this.ySpeed = 0;

        if (this.assignedWeapons.length == 0) {
            // 一开始没有默认武器就从未选用武器里随机选一个
            this.addWeaponByIndex(Math.floor(Math.random() * this.unassignedWeapons.length));
        } else {
            // 有就激活
            for (let i = 0; i < this.assignedWeapons.length; i++) {
                this.assignedWeapons[i].node.active = true;
            }
        }
        this.fullLevelWeapons = [];

        this.maxSpeed = require("PlayerStatController").instance.moveSpeed[0].value;
        this.pickUpRange = require("PlayerStatController").instance.pickUpRange[0].value;
        this.maxWeaponNumber = require("PlayerStatController").instance.maxWeapons[0].value;
    },

    onKeyDown(event) {
        switch (event.keyCode) {
            case cc.macro.KEY.w:
                this.moveUp = true;
                break;
            case cc.macro.KEY.s:
                this.moveDown = true;
                break;
            case cc.macro.KEY.a:
                this.moveLeft = true;
                break;
            case cc.macro.KEY.d:
                this.moveRight = true;
                break;

        }

        return;
    },

    onKeyUp(event) {
        switch (event.keyCode) {
            case cc.macro.KEY.w:
                this.moveUp = false;
                break;
            case cc.macro.KEY.s:
                this.moveDown = false;
                break;
            case cc.macro.KEY.a:
                this.moveLeft = false;
                break;
            case cc.macro.KEY.d:
                this.moveRight = false;
                break;
        }
    },

    update (dt) {

        this.regularMove(dt);

        let sprite = this.node.getChildByName('Sprite');
        if (this.moveLeft) {
            sprite.scaleX = -1;
        } else if (this.moveRight) {
			sprite.scaleX = 1;
		}
    },

    onDestroy () {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    getMoveVector() {
        let moveVector = cc.v2(0, 0);
        //let Right = cc.v2(1,0);
        if (this.moveUp) {
            moveVector.addSelf(cc.v2(0, 1));
        }
        if (this.moveDown) {
            moveVector.addSelf(cc.v2(0, -1));
        }
        if (this.moveLeft) {
            moveVector.addSelf(cc.v2(-1, 0));
        }
        if (this.moveRight) {
            moveVector.addSelf(cc.v2(1, 0));
        }
        return moveVector.normalizeSelf();
    },

    regularMove(dt) {
        let MV = this.getMoveVector();
        //console.log(MV);
        this.xSpeed = MV.x * this.speed;
        this.ySpeed = MV.y * this.speed;

        this.node.x += MV.x * this.speed * dt;
        this.node.y += MV.y * this.speed * dt;
        return;
    },

    getPlayerDrgree() {
        let drgree = 0;
        if (this.ySpeed === 0 && this.xSpeed === 0) {
            drgree = null;
        }
        else if (this.ySpeed === 0) {
            if (this.xSpeed > 0) {
                drgree = 90;
            }
            else if (this.xSpeed < 0) {
                drgree = 270;
            }
        }
        else if (this.xSpeed === 0) {
            if (this.ySpeed > 0) {
                drgree = 0;
            }
            else if (this.ySpeed < 0) {
                drgree = 180;
            }
        }
        else {
            let tanRadians = this.xSpeed / this.ySpeed;
            if (this.xSpeed > 0 && this.ySpeed > 0) {
                drgree = cc.misc.radiansToDegrees(Math.atan(tanRadians));
            }
            else if (this.xSpeed > 0 && this.ySpeed < 0) {
                drgree = cc.misc.radiansToDegrees(Math.atan(tanRadians)) + 180;
            }
            else if (this.xSpeed < 0 && this.ySpeed < 0) {
                drgree = cc.misc.radiansToDegrees(Math.atan(tanRadians)) + 180;
            }
            else if (this.xSpeed < 0 && this.ySpeed > 0) {
                drgree = cc.misc.radiansToDegrees(Math.atan(tanRadians)) + 360;
            }
        }
        return drgree;
    },

    addWeaponByIndex (weaponIndex) {
        if (weaponIndex < this.unassignedWeapons.length) {
            this.assignedWeapons.push(this.unassignedWeapons[weaponIndex]);

            this.unassignedWeapons[weaponIndex].node.active = true;
            this.unassignedWeapons.splice(weaponIndex, 1);
        }
    },

    addWeaponByWeapon (weaponToAdd) {
        weaponToAdd.node.active = true;

        this.assignedWeapons.push(weaponToAdd);
        this.unassignedWeapons.splice(this.unassignedWeapons.indexOf(weaponToAdd), 1);
    },
});

module.exports = PlayerController;
